package com.hebust.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hebust.entity.Teachers;
import com.hebust.mapper.TeacherMapper;
import com.hebust.mapper.TeachersMapper;
import com.hebust.service.TeachersService;
import com.hebust.utils.ResultVo;
import com.hebust.utils.student.ScoreSearch;
import com.hebust.utils.teacher.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TeachersServiceImpl implements TeachersService {

    @Autowired
    private TeachersMapper teachersMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public ResultVo login(String username, String password) {
//        Teachers teachers = teachersMapper.login(username);
        Teachers t = new Teachers();
        t.setTNumber(username);
        Teachers teachers = teachersMapper.selectOne(t);
        if (null == teachers) {
            return new ResultVo(1000, "用户名不存在", null);
        } else {
            if (teachers.getTPassword().equals(password)) {
                return new ResultVo(1002, "登录成功", teachers);
            } else {
                return new ResultVo(1001, "密码错误", null);
            }
        }
    }

    @Override
    public ResultVo my_score(int tid) {
        List<MyScore> myScore = teacherMapper.my_score(tid);
        if (myScore != null) {
            return new ResultVo(1002, "成功", myScore);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo update_teacherpassword(int tid, String password) {
        int i = teacherMapper.update_teacherpassword(tid, password);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo show_studentscore(int tid, String cname, String sname, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<StudentScore> studentScores = teacherMapper.show_studentscore(tid, cname, sname);
        PageInfo page = new PageInfo(studentScores);
        if (studentScores.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);
    }

    @Override
    public ResultVo add_studentscore(int id, int score) {
        int i = teacherMapper.add_studentscore(id, score);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo show_teachercourse(int tid, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<Teach> teaches = teacherMapper.show_teachercourse(tid);
        PageInfo page = new PageInfo(teaches);
        if (teaches.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);
    }

    @Override
    public ResultVo edit_courseinformation(int id, String etime, String elocation) {
        Integer i = teacherMapper.edit_courseinformation(id, etime, elocation);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    // 课表
    @Override
    public ResultVo show_teachercourseschedule(int tid) {
        List<TeacherSchedule> teacherSchedules = teacherMapper.show_teachercourseschedule(tid);
        List<JSONObject> list = new ArrayList<>();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        JSONObject jsonObject3 = new JSONObject();
        JSONObject jsonObject4 = new JSONObject();
        jsonObject1.put("section", "第一大节");
        jsonObject1.put("time", "08:00-09:40");
        jsonObject2.put("section", "第二大节");
        jsonObject2.put("time", "10:00-11:40");
        jsonObject3.put("section", "第三大节");
        jsonObject3.put("time", "13:40-15:20");
        jsonObject4.put("section", "第四大节");
        jsonObject4.put("time", "15:40-17:20");
        list.add(jsonObject1);
        list.add(jsonObject2);
        list.add(jsonObject3);
        list.add(jsonObject4);

        teacherSchedules.forEach(res -> {
            TeacherScheduleObj teacherScheduleObj = new TeacherScheduleObj();
            teacherScheduleObj.setName(res.getCname());
            teacherScheduleObj.setLocation(res.getLocation());
            String select = res.getCtime().split(";")[0].split(" ")[1];
            switch (select) {
                case "第一大节":
                    jsonObject1.put(res.getCtime().split(";")[0].split(" ")[0], teacherScheduleObj);
                    break;
                case "第二大节":
                    jsonObject2.put(res.getCtime().split(";")[0].split(" ")[0], teacherScheduleObj);
                    break;
                case "第三大节":
                    jsonObject3.put(res.getCtime().split(";")[0].split(" ")[0], teacherScheduleObj);
                    break;
                case "第四大节":
                    jsonObject4.put(res.getCtime().split(";")[0].split(" ")[0], teacherScheduleObj);
                    break;
            }
            String select1 = res.getCtime().split(";")[1].split(" ")[1];
            switch (select1) {
                case "第一大节":
                    jsonObject1.put(res.getCtime().split(";")[1].split(" ")[0], teacherScheduleObj);
                    break;
                case "第二大节":
                    jsonObject2.put(res.getCtime().split(";")[1].split(" ")[0], teacherScheduleObj);
                    break;
                case "第三大节":
                    jsonObject3.put(res.getCtime().split(";")[1].split(" ")[0], teacherScheduleObj);
                    break;
                case "第四大节":
                    jsonObject4.put(res.getCtime().split(";")[1].split(" ")[0], teacherScheduleObj);
                    break;
            }
        });

        return new ResultVo(1002, "成功", list);
    }


}
